package com.zs.usc.controller;


import com.zs.usc.common.BaseAuthController;
import com.zs.usc.common.RestRst;
import com.zs.usc.common.validate.MyValidate;
import com.zs.usc.constant.PermsConstraint;
import com.zs.usc.entity.MenuInfo;
import com.zs.usc.security.Authorization;
import com.zs.usc.service.IMenuService;
import com.zs.usc.vo.CurrentUserInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ZS
 * @since 2021-12-11
 */
@RestController
@RequestMapping("/menu")
@Api(tags = "菜单模块", value = "菜单模块")
public class MenuController extends BaseAuthController {


    @Autowired
    private IMenuService menuService;


    @GetMapping("/listMenuByUserId")
    @ApiOperation(value = "获取当前用户菜单列表信息")
    @Authorization
    public RestRst<List<MenuInfo>> listMenuByUserId(
            @ApiParam("用户ID") @RequestParam String userId,
            @ApiParam("应用标识") @RequestParam(required = false) String appCode) {
        // 获取当前用户的登录信息
        return RestRst.buildSuccess(menuService.listMenuByUserId(userId, appCode));
    }


    @GetMapping("/listMenu")
    @ApiOperation(value = "获取当前用户菜单列表信息")
    @Authorization
    public RestRst<List<MenuInfo>> listMenu(@ApiParam("应用标识") @RequestParam(required = false) String appCode) {
        // 获取当前用户的登录信息
        CurrentUserInfo currentUser = this.getCurrentUser();
        return RestRst.buildSuccess(menuService.listMenu(currentUser, appCode));
    }


    @ApiOperation(value = "过滤当前用户给定的菜单哪些具有权限", notes = "过滤当前用户给定的菜单哪些具有权限")
    @RequestMapping(path = "/hasList", method = RequestMethod.POST)
    @Authorization
    public RestRst getUserAllowMenus(@RequestBody List<String> menuList) {
        CurrentUserInfo u = this.getCurrentUser();
        return RestRst.buildSuccess(menuService.getUserAllowMenus(u, menuList));
    }

    @PostMapping("/saveMenu")
    @ApiOperation(value = "新增菜单信息")
    @Authorization(value = PermsConstraint.SEC_SYSTEM_MENU_ADD)
    @MyValidate
    public RestRst<Integer> saveMenu(@RequestBody MenuInfo menuInfo) {
        return RestRst.buildSuccess(this.menuService.save(menuInfo));
    }
}

